package com.fs.dao;

import java.awt.Color;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;


import com.fs.entity.Oder;
import com.fs.entity.Receipt;
import com.fs.utils.DBUtils;

public class OderDao {

	public  int add(Oder oder) {
		String sql="insert into tb_order(user_id,oder_price,oder_number,receipt_name,receipt_phone,receipt_addr) values(?,?,?,?,?,?)";		
		Connection con=null;
		PreparedStatement ps=null;
		ResultSet rs=null;
		int oder_id=0;
		try {
			con=DBUtils.getConnection();
			
			ps = con.prepareStatement(sql,Statement.RETURN_GENERATED_KEYS); //返回保存对象的主键
			//ps = con.prepareStatement(sql);
			ps.setInt(1,oder.getUser_id());			
			ps.setString(2,oder.getoder_price());
			ps.setString(3, String.valueOf((int)((Math.random()*9+1)*100000000)));
			ps.setString(4, oder.getReceipt_name());
			ps.setString(5, oder.getReceipt_phone());
			ps.setString(6, oder.getReceipt_addr());
		    ps.executeUpdate();
		    rs= ps.getGeneratedKeys();  
            while(rs.next()){
            	oder_id = rs.getInt(1);
            }
			
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			DBUtils.close(con, rs, null, ps);
		}

		return oder_id;
		
	
	}
	public static  List<Oder> getOderList(int user_id) {
		Connection con=null;
		PreparedStatement ps=null;
		ResultSet rs=null;
		List<Oder> list=new ArrayList<Oder>();
		String sql="select * from tb_order where user_id=?";	
		try {
			con=DBUtils.getConnection();//获取数据库的连接
			ps=con.prepareStatement(sql);
			ps.setInt(1, user_id);
			rs=ps.executeQuery();
			Oder oder=null;
			while(rs.next()){
				oder=new Oder();
				oder.setUser_id(rs.getInt("user_id"));
				oder.setoder_price(rs.getString("oder_price"));
				oder.setoder_number(rs.getString("oder_number"));
				oder.setReceipt_name(rs.getString("receipt_name"));
				oder.setReceipt_phone(rs.getString("receipt_phone"));
				oder.setReceipt_addr(rs.getString("receipt_addr"));
				list.add(oder);
			}
		
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			DBUtils.close(con, rs, null, ps);
		}
		return list;
		
	
	}
	public static Oder getOderById(int oder_id) {
		Connection con=null;
		PreparedStatement ps=null;
		ResultSet rs=null;
		Oder oder=new Oder();
		String sql="select * from tb_order where oder_id=?";	
		try {
			con=DBUtils.getConnection();//获取数据库的连接
			ps=con.prepareStatement(sql);
			ps.setInt(1, oder_id);
			rs=ps.executeQuery();
			while(rs.next()){
				oder=new Oder();
				oder.setoder_id(rs.getInt("oder_id"));
				oder.setoder_price(rs.getString("oder_price"));
				oder.setoder_number(rs.getString("oder_number"));
				oder.setReceipt_name(rs.getString("receipt_name"));
				oder.setReceipt_phone(rs.getString("receipt_phone"));
				oder.setReceipt_addr(rs.getString("receipt_addr"));
				
			}
		
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			DBUtils.close(con, rs, null, ps);
		}
		return oder;
		
		
		
		
		
		
	}

	
}
	
	
	


